const puppeteer = require('puppeteer');
// const URL = 'http://fund.eastmoney.com/data/fundranking.html';
const URL = 'http://fund.eastmoney.com/data/fundranking.html#tgp;c0;r;szzf;pn50;ddesc;qsd20180311;qed20190311;qdii;zq;gg;gzbd;gzfs;bbzt;sfbb'
const axios = require('axios')
const cheerio = require('cheerio')
puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }).then(async browser => {
    const page = await browser.newPage();
    await page.setViewport({width: 320, height: 600})
    // await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 9_0_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13A404 Safari/601.1')

    await page.goto(URL, {waitUntil: 'networkidle0'});
    await page.waitForSelector('#dbtable');
    await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})
    const result = await page.evaluate(() => {
        try {
            var data = [];
            $('#dbtable tbody tr').each(function() {
                const $a = $(this).find('td:nth-child(4) a')
                const url = $a.attr('href');
                const title =$a.attr('title')
                data.push({
                    'title' : title,
                    'url'   : url
                });
            });
            return data; // 返回数组
        } catch(err) {
            console.log(err)
        }
    });
    let gpObjs = {}
    let axiosAll = []
    for(let i = 0,len=result.length; i<len; i++){
        axiosAll.push(axios.get(result[i].url))
        
    }
    axios.all(axiosAll)
        .then(axios.spread(function(...args){
            args.forEach(function(item, i){
                const $ = cheerio.load(item.data)
                $('#position_shares .ui-table-hover tr:not(:first-child) td:first-child a').each(function(i,e){
                    gpObjs[$(this).text()] = gpObjs[$(this).text()] ? ++gpObjs[$(this).text()] : 1
                })
            })
            let sorted = Object.keys(gpObjs).sort(function(a,b){
                return gpObjs[b] - gpObjs[a]
            })
            console.log(sorted)
            for(let i, len=sorted.length; i<len;i++){
                
            }
        }))
    // 记录播客标题
    
    
/**
 
.then(data => {
                    const $ = cheerio.load(data.data)
                    $('#position_shares .ui-table-hover tr:not(:first-child) td:first-child a').each(function(i,e){
                        gpObjs[$(this).text()] = gpObjs[$(this).text()] ? ++gpObjs[$(this).text()] : 1
                    })
                })
                .catch(e => {
                    console.log(e)
                })

 * 
 */


    // var gps = []
    // for(var i = 0; i < 1; i++) {
    //     // console.log('Title: ' + result[i].title + ' URL: ' + result[i].url);
    //     axios.get(result[i].url)
    //         .then((result)=>{
    //             console.log(result.data)
    //         })
    //         .catch((err)=>{
    //             console.log(err)
    //         })
        // await page.goto(result[i].url, {waitUntil: 'networkidle0'})
        // await page.waitForSelector('.ui-table-hover');
        // const aaa = await page.evaluate(()=>{
        //     console.log(111111)
        //     try {
        //         console.log(2222)
        //         const $gu = $(".ui-table-hover tr:not(:first-child) td:first-child a")
        //         const tit = $gu.attr("title")
        //         console.log(tit)
        //         gps.push(tit)
        //         return tit
        //     }catch(e){
        //         console.log(e)
        //     }
        // })
    // }
    // console.log(gps)
    // 关闭浏览器
    await browser.close();

    
    // process.exit();
}).catch(function(error) {
    console.error('No way Paco!');
    process.exit();
});

